package com.yikes.srb.controller;

import com.yikes.srb.common.result.Result;
import com.yikes.srb.service.FileService;
import com.yikes.srb.vo.FileInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 * 文件接口
 * </p>
 *
 * @Author: guanrong.yin
 * @Date: 2024/12/10 23:27
 */
@Tag(name = "FileController", description = "文件接口")
@RestController
@RequestMapping("/v2/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @Operation(description = "列表")
    @GetMapping("/listItem")
    public Result<List<Object>> listItem() {

        List<Object> list = fileService.listItem();

        return Result.ok(list);
    }

    @PostMapping("/upload")
    @Operation(description = "文件上传")
    public Result<List<FileInfoVO>> uploadFile(@Schema(description = "表单文件对象")
                                               @RequestParam(value = "file") MultipartFile[] file) {

        if (file == null || 0 == file.length) {
            return Result.fail("上传文件不能为空");
        }

        List<FileInfoVO> fileInfo = fileService.uploadFile(file);
        return Result.ok(fileInfo);
    }

    @DeleteMapping("/remove")
    @Operation(description = "文件删除")
    @SneakyThrows
    public Result deleteFile(@Schema(description = "文件路径") @RequestParam String filePath) {

        fileService.deleteFile(filePath);
        return Result.ok();
    }

    @Operation(description = "下载")
    @GetMapping("/download_file")
    public void downloadFileName(HttpServletResponse response, @RequestParam("fileName") String fileName) {

        fileService.downloadFileName(response, fileName);
    }


}
